﻿using GodSystemBLL;
using GodSystemCommon;
using GodSystemModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormProductList : Form
    {
        public FormProductList()
        {
            InitializeComponent();
            InitPower();
            GetProductList();
        }


        /// <summary>
        /// 设置权限可见性
        /// </summary>
        private void InitPower()
        {
            // 设置坐标
            int x = 430;
            int y = 22;

            // 按钮间隔
            int sep = 36;

            // 设置大小
            int sizeWidth = 90;
            int sizeHeight = 30;
            // 添加产品
            if (!Utility.CheckUserPower("ProductAdd"))
            {
                button_Add.Visible = false;
            }
            else
            {
                button_Add.Location = new Point(x, y);
                button_Add.Size = new Size(sizeWidth, sizeHeight);
                x += sep + sizeWidth;
            }

            // 修改产品
            if (!Utility.CheckUserPower("ProductUpdate"))
            {
                button_Update.Visible = false;
            }
            else
            {
                button_Update.Location = new Point(x, y);
                button_Update.Size = new Size(sizeWidth, sizeHeight);
                x += sep + sizeWidth;
            }



            // 删除产品
            if (!Utility.CheckUserPower("ProductDelete"))
            {
                button_Delete.Visible = false;
            }
            else
            {
                button_Delete.Location = new Point(x, y);
                button_Delete.Size = new Size(sizeWidth, sizeHeight);
                x += sep + sizeWidth;
            }

            // 产品批量添加
            if (!Utility.CheckUserPower("ProductAddbBatch"))
            {
                button_ProductAddBatch.Visible = false;
            }
            else
            {
                button_ProductAddBatch.Location = new Point(x, y);
                button_ProductAddBatch.Size = new Size(sizeWidth, sizeHeight);
            }

        }

        Hashtable cateMap = new Hashtable();

        /// <summary>
        /// 获取产品列表
        /// </summary>
        private void GetProductList()
        {
            ProductsBLL productsBLL = new ProductsBLL();
            ProductsModel productsModel = new ProductsModel();
            DataTable productList = productsBLL.GetProductsList(1, 10, productsModel); 
            if(productList.Rows.Count > 0)
            {
                
                // 防止图片失真
                imageList1.ColorDepth = ColorDepth.Depth32Bit;
                imageList1.ImageSize = new Size(40, 40);

                dataGridView_ProductList.AutoResizeColumns();

                // 获取分类
                CateBLL cateBLL = new CateBLL();
                DataTable cateList = cateBLL.GetCategories();
                if(cateList.Rows.Count > 0)
                {
                    for(int i = 0; i < cateList.Rows.Count; i++)
                    {
                        cateMap.Add(cateList.Rows[i].Field<uint>("Id").ToString(), cateList.Rows[i].Field<string>("CateName"));
                    }
                }

                // 记录遍历过的供应商
                Hashtable supplierList = new Hashtable();

                // 产品状态
                Hashtable productStatusType = new Hashtable();
                productStatusType.Add(0, "草稿");
                productStatusType.Add(1, "正常");
                productStatusType.Add(2, "冻结");

                // 0 产品编号
                // 1 产品编码
                // 2 产品图
                // 3 产品状态
                // 4 产品名称
                // 5 产品主编号
                // 6 产品分类
                // 7 产品重量
                // 8 产品价格
                // 9 供应商

                for (int i = 0; i < productList.Rows.Count; i++)
                {
                    // 0 产品编号
                    uint id = productList.Rows[i].Field<uint>("Id");
                    // 1 产品编码
                    string productSn = productList.Rows[i].Field<string>("ProductSn").ToString();
                    // 2 产品图
                    string picPath = productList.Rows[i].Field<string>("ProductThumbImage").ToString();

                    // 3 产品状态.
                    // mysql中为 tinyint
                    // bool productStatus = productList.Rows[i].Field<bool>("ProductStatus");
                    int productStatus = productList.Rows[i].Field<int>("ProductStatus");
                    // 4 产品名称
                    string productName = productList.Rows[i].Field<string>("ProductName").ToString();
                    // 5 产品主编号
                    string mainSku = productList.Rows[i].Field<string>("MainSku").ToString();
                    // 6 产品分类
                    int cateId = productList.Rows[i].Field<int>("CateId");
                    // 7 产品重量
                    decimal weight = productList.Rows[i].Field<decimal>("Weight");
                    // 8 产品价格
                    decimal purchasePrice = productList.Rows[i].Field<decimal>("PurchasePrice");
                    // 9 供应商
                    string supplierId = productList.Rows[i].Field<int>("SupplierId").ToString();
                    

                    // select Id,ProductName,ProductSn,SupplierId,ProductStatus,CreateTime,CateId,MainSku,Weight,PurchasePrice,ProductThumbImage
                    // imageList 控件保存对应的图片
                    GetProductImage(picPath);

                    DataGridViewRow row = new DataGridViewRow();

                    // 0 产品编号
                    DataGridViewTextBoxCell textId = new DataGridViewTextBoxCell();
                    textId.Value = id;
                    row.Cells.Add(textId);

                    // 1 产品编码
                    DataGridViewTextBoxCell textProductSn = new DataGridViewTextBoxCell();
                    textProductSn.Value = productSn;
                    row.Cells.Add(textProductSn);

                    // 2 产品图
                    DataGridViewImageCell imageId = new DataGridViewImageCell();
                    imageId.Value = imageList1.Images[i];
                    row.Cells.Add(imageId);

                    // 3 产品状态.
                    DataGridViewTextBoxCell textStatus = new DataGridViewTextBoxCell();
                    if (productStatusType.ContainsKey(productStatus))
                    {
                        textStatus.Value = productStatusType[productStatus];
                    }
                    else
                    {
                        textStatus.Value = "未设置";
                    }
                    
                    row.Cells.Add(textStatus);

                    // 4 产品名称
                    DataGridViewTextBoxCell textName = new DataGridViewTextBoxCell();
                    textName.Value = productName;
                    row.Cells.Add(textName);

                    // 5 产品主编号
                    DataGridViewTextBoxCell textMainSku = new DataGridViewTextBoxCell();
                    textMainSku.Value = mainSku;
                    row.Cells.Add(textMainSku);

                    // 6 产品分类
                    DataGridViewTextBoxCell textCateId = new DataGridViewTextBoxCell();
                    if (cateMap.ContainsKey(cateId))
                    {
                        textCateId.Value = cateMap[cateId];
                    }
                    else 
                    {
                        textCateId.Value = "未分类";
                    }
                    
                    row.Cells.Add(textCateId);

                    // 7 产品重量
                    DataGridViewTextBoxCell textWeight = new DataGridViewTextBoxCell();
                    textWeight.Value = weight;
                    row.Cells.Add(textWeight);

                    // 8 产品价格
                    DataGridViewTextBoxCell textPurchasePrice = new DataGridViewTextBoxCell();
                    textPurchasePrice.Value = purchasePrice;
                    row.Cells.Add(textPurchasePrice);

                    // 9 供应商
                    DataGridViewTextBoxCell textSupplierId = new DataGridViewTextBoxCell();
                    if (supplierList.ContainsKey(supplierId))
                    {
                        textSupplierId.Value = supplierList[supplierId];
                    }
                    else
                    {
                        SupplierBLL supplierBLL = new SupplierBLL();
                        SupplierModel supplierInfo = supplierBLL.GetSupplierById(Convert.ToUInt32(supplierId));

                        if (supplierInfo != null)
                        {
                            supplierList.Add(supplierInfo.Id, supplierInfo.SupplierName);
                            textSupplierId.Value = supplierInfo.SupplierName;
                        }
                        else
                        {
                            textSupplierId.Value = "未设置";
                        }
                    }

                    row.Cells.Add(textSupplierId);

                    dataGridView_ProductList.Rows.Add(row);
                }

            }

            
        }

        /// <summary>
        /// 无图片展示
        /// </summary>
        private void SetNoneImage()
        {
            // 无图片时，为了不受路径影响，在设计页面
            // 添加一个控件，然后添加对应的图片
            // 此时的图片写到资源里面，即直接从资源读取
            Image img = Properties.Resources.no_pic;
            imageList1.Images.Add(img);
        }

        private void GetProductImage(string fileName = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                SetNoneImage();
            }
            else
            {
                string pro = null;
                if (fileName.Length > 4)
                {
                     pro = fileName.Substring(0, 4).ToLower();
                }
                // URL图片
                if (pro.Equals("http"))
                {
                    try
                    {
                        Image img = Image.FromStream(System.Net.WebRequest.Create(fileName).GetResponse().GetResponseStream());
                        imageList1.Images.Add(img);
                    }
                    catch (Exception)
                    {
                        SetNoneImage();
                    }
                }
                else
                {
                    // 本地
                    // 程序当前目录
                    string currentDir = Environment.CurrentDirectory;

                    string rootPath = currentDir.Substring(0, currentDir.LastIndexOf("\\"));
                    // .../GodSystem 检测是否在编辑器中运行
                    int preLen = rootPath.LastIndexOf("\\");
                    if (preLen > 0)
                    {
                        rootPath = rootPath.Substring(0, rootPath.LastIndexOf("\\"));
                        fileName = rootPath + "\\" + fileName;
                        try
                        {
                            Image img = Image.FromFile(fileName);
                            imageList1.Images.Add(img);
                        }
                        catch (Exception)
                        {
                            SetNoneImage();
                        }
                    }
                    else
                    {
                        SetNoneImage();
                    }
                }
            }


        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            FormProductAdd product = new FormProductAdd();
            product.Activate();
            product.Show();
        }

        private void button_ProductAddBatch_Click(object sender, EventArgs e)
        {
            FormProductAddBatch productBatch = new FormProductAddBatch();
            productBatch.Activate();
            productBatch.Show();
        }
    }



}
